package common.playerUpgrade.view
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	
	import common.playerUpgrade.IPlayerUpgradeView;
	import common.playerUpgrade.item.PlayerUpgradeItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import lz.controls.LScrollBar;
	import lz.controls.core.UIComponent;
	import lz.controls.events.ScrollEvent;
	import lz.controls.managers.AnimationManager;
	import lz.controls.nick.PagePanel;

	/**
	 *球员升级视图 
	 * @author lake
	 * 
	 */	
	public class PlayerUpgradeView extends UIComponent implements IPlayerUpgradeView
	{
		/**
		 * 关闭按钮
		 */		
		protected var closeBtn 	:SimpleButton;
		/**
		 * 球员容器
		 */		
		protected var container	:MovieClip;
		/**
		 *数据列表 
		 */		
		protected var dataList		:Array;
		/**
		 *item 列表 
		 */		
		protected var itemList		:Array = [];
		/**
		 *滚动条 
		 */		
		protected var scrollBar 	:LScrollBar;
		/**
		 * 
		 */
		private var _page:PagePanel;
		
		public function PlayerUpgradeView()
		{
			super();
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.lz.res.PlayerUpgradePanel");
			this.addChild(_symbolSkin);
			_symbolSkin.gotoAndStop(1);
//			_symbolSkin.x = 140;
//			_symbolSkin.y = 73;
			closeBtn 	= _symbolSkin.mc.closeBtn;
			container 	= _symbolSkin.mc.container;
			_page = new PagePanel();
			_page.initUI(_symbolSkin.mc.page);
			_page.callBack = turnPage;
			
			//
			addEvtListener();
//			_symbolSkin.gotoAndPlay(1);
		}
		/**
		 * set data 
		 * @param value
		 * 
		 */		
		public function set data(value:*):void
		{
			dataList = value;
			_page.max = Math.ceil(dataList.length / 5);
			//set data
			var item 	:PlayerUpgradeItem;
			for(var i:int = 0;i < 5;i++)
			{
				item = new PlayerUpgradeItem();
				item.initUI();
//				container.addChild(item);
				item.y = i * 60;
				itemList.push(item);
			}
			turnPage();
//			for each(var playerInfo :PlayerVo in dataList)
//			{
//				item = new PlayerUpgradeItem();
//				item.initUI();
//				item.data = playerInfo;
//				container.addChild(item);
//				item.y = i * 60;
//				itemList.push(item);
//				i ++ ;
//			}
//			if(i > 4)
//			{
//				scrollBar = new LScrollBar();
//				_symbolSkin.mc.addChild(scrollBar);
//				scrollBar.x = 467;
//				scrollBar.y = 134;
//				scrollBar.height = 257;
//				scrollBar.lineScrollSize = 60;
//				scrollBar.maxScrollPosition = container.height - scrollBar.height + 17;
//				container.scrollRect = new Rectangle(0,0,container.width,240);
//				scrollBar.addEventListener(ScrollEvent.SCROLL,onScrollHandler);
//				function onScrollHandler(e :ScrollEvent):void
//				{
//					var rect :Rectangle = container.scrollRect;
//					rect.y = e.position;
//					container.scrollRect = rect;
//				}
//			}
			_symbolSkin.gotoAndPlay(1);
		}
		/**
		 *换页  
		 * @param type
		 * 
		 */
		private function turnPage(type:String = ""):void
		{
			while(container.numChildren)
			{
				container.removeChildAt(0);
			}
			var arr:Array = ArrayUtil.getPageSizeDatas(_page.min,5,dataList);
			var len:int = arr.length;
			var item 	:PlayerUpgradeItem;
			for(var i:int = 0;i < len;i++)
			{
				item = itemList[i];
				item.data = arr[i];
				container.addChild(item);
			}
		}
	
		/**
		 * get data
		 * @return 
		 * 
		 */		
		public function get data():*
		{
			return dataList;
		}
		/**
		 * 添加事件
		 * 
		 */		
		public function addEvtListener():void
		{
			closeBtn.addEventListener(MouseEvent.CLICK,onCloseHandler);
		}
		/**
		 *click closeBtn 
		 * @param e
		 * 
		 */		
		private function onCloseHandler(e :MouseEvent):void
		{
			AppData.getInstance().trainingCampDataManager.upPlayerArr.splice(0,AppData.getInstance().trainingCampDataManager.upPlayerArr.length);
			AnimationManager.remove();
			if(this.parent)
			{
				destroyView();
			}
		}
		/**
		 * 
		 * 删除事件
		 */		
		public function removeEvtListener():void
		{
			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			DisplayObjectUtil.removeAllChildren(this);
			DisplayObjectUtil.removeAllChildren(container);
			for each(var item:PlayerUpgradeItem in itemList)
			{
				item.destroyView();
				item = null;
			}
			itemList = null;
			scrollBar = null;
			dataList = null;
		}
		/**
		 *显示类型 
		 * @param $obj
		 * 
		 */
		public function showType($obj:Object):void
		{
			switch($obj.type)
			{
				
			}
		}
	}
}